package factory;

import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import config.DisruptorProperties;
import util.WaitStrategys;

public class DisruptorFactory<T> {

    private DisruptorProperties disruptorProperties;
    private EventThreadFactory eventThreadFactory;

    public DisruptorFactory(DisruptorProperties disruptorProperties,EventThreadFactory eventThreadFactory) {
        this.disruptorProperties = disruptorProperties;
        this.eventThreadFactory = eventThreadFactory;
    }

    public Disruptor<T> getInstance(){
        if(disruptorProperties.getMultiProducer()){
            return new Disruptor<T>(new EventEntityFactory<T>(),
                    disruptorProperties.getRingBufferSize(),
                    eventThreadFactory,ProducerType.MULTI,new YieldingWaitStrategy());
        }else{
            return new Disruptor<T>(new EventEntityFactory<T>(),
                    disruptorProperties.getRingBufferSize(),
                    eventThreadFactory,ProducerType.SINGLE,new YieldingWaitStrategy());
        }

    }

    public Disruptor<T> getInstance(WaitStrategy waitStrategy){
        if(disruptorProperties.getMultiProducer()){
            return new Disruptor<T>(new EventEntityFactory<T>(),
                    disruptorProperties.getRingBufferSize(),
                    eventThreadFactory,ProducerType.MULTI,waitStrategy);
        }else{
            return new Disruptor<T>(new EventEntityFactory<T>(),
                    disruptorProperties.getRingBufferSize(),
                    eventThreadFactory,ProducerType.SINGLE,waitStrategy);
        }

    }


}